package com.itbour.web.action.user;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.user.GetOssFileListUtil;
import com.itbour.service.user.UserInfoService;
import com.opensymphony.xwork2.ActionContext;


/**
 *	 @author LQ 
 *
 * 	   用户信息管理
 */
public class GetUserInfoDtoAction extends BaseAction {

	/**
	 */
	private static final long serialVersionUID = 1L;
	private UserInfoService userInfoService;
	private String findMobile;
	private String findName;
	private String findId;
	private String findType;
	private Map<String, Object> mapResult=new HashMap<String, Object>();
	private List<UserInfoDto> listUser;
	private UserInfoDto userInfo;
	private String findIsPay;// 用户是否买个单
	
	
	/**	分页获取用户信息*/
	public String pageUserInfo(){
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		HttpServletRequest request=ServletActionContext.getRequest();
		if(user!=null && user.getUser_id()!=null){
			Integer pageNo = Integer.parseInt(request.getParameter("page"));
			Integer pageSize =Integer.parseInt(request.getParameter("rows"));
			Map<String,Object> parm=new HashMap<String, Object>();
			if(StringUtils.isNotEmpty(findMobile)){
				parm.put("mobile", findMobile);
			}
			if(StringUtils.isNotEmpty(findName)){
				parm.put("nickname", findName);
			}
			if (StringUtils.isNotEmpty(findId)) {
				parm.put("user_id", findId);
			}
			if(StringUtils.isNotBlank(findType)){
				parm.put("user_type",findType);
			}
			if(StringUtils.isNotBlank(findIsPay)){
				parm.put("isPay",findIsPay);
			}
			Page page=userInfoService.getUserInfo(parm, pageNo, pageSize);
			mapResult.put("total", page.getTotalCount());
			mapResult.put("rows",page.getResult());
		}
		return SUCCESS;
	}
	
	public String getOneUserInfo(){
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null&&user.getUser_id()!=null){
			HttpServletRequest request=ServletActionContext.getRequest();
			Integer user_id=Integer.parseInt(request.getParameter("id"));
			Map<String, Object> parm=new HashMap<String, Object>();
			parm.put("user_id",user_id);
			listUser=userInfoService.getOneUserInfo(parm);
		}
		return SUCCESS;
	}
	
	//修改用户信息
	public void  updUserInfo() throws Exception{
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null&&user.getUser_id()!=null){
			try{
				userInfo.setUpdate_by(user.getUser_id().toString());
				userInfoService.updUserInfo(userInfo);
				this.writeJSON("ok");
			}catch(Exception e){
				this.writeJSON("no");
			}
		}
	}

	/**
	 * 获取一键生成 用户统计
	 * author  liqi
	 * 参数：按周
	 * 参数：起始和截止日期
	 * @return
	 */
	private String findDateStart;
	private String findDateEnd;// 结束时间
	private String findSdt;// 统计状态 按日周月
	private String findMobStat;// 有无手机号用户
	public String getYjscUserStats(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if (user !=null && user.getUser_id()!=null){
			HttpServletRequest request=ServletActionContext.getRequest();
			Integer pageNo = Integer.parseInt(request.getParameter("page"));
			Integer pageSize =Integer.parseInt(request.getParameter("rows"));
			Map<String,Object> parm=new HashMap<String, Object>();
			if(StringUtils.isNotEmpty(findDateStart)){
				parm.put("crtDateStart", findDateStart);
			}
			if (StringUtils.isNotEmpty(findDateEnd)) {
				parm.put("crtDateEnd", findDateEnd);
			}
			if(StringUtils.isNotBlank(findSdt)){
				parm.put("grpBy",findSdt);
			}
			if(StringUtils.isNotEmpty(findMobStat)){
				parm.put("mobStat",findMobStat);
			}
			Page page=userInfoService.getUserStats(parm, pageNo, pageSize);
			mapResult.put("total", page.getTotalCount());
			mapResult.put("rows",page.getResult());
		} else {
			mapResult.put("total", 0);
			mapResult.put("rows", 0);
		}
		return SUCCESS;
	}
	
	
	/*
	 * 分页获取 文件夹信息
	 */
	private String findNextMark;
	private String findPrefix;
	public String pageGetOssFile(){
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			Integer pageNo = 1;
			Integer pageSize =10;
			Map<String,Object> parm=new HashMap<String, Object>();
			if(StringUtils.isNotEmpty(findNextMark)){
				parm.put("nextMarker", findNextMark);
			}
			if(StringUtils.isNotEmpty(findPrefix)){
				parm.put("prefix", findPrefix);
			}
			Page page=userInfoService.getOssFileList(parm, pageNo, pageSize);
			mapResult.put("total", page.getTotalCount());
			mapResult.put("rows",page.getResult());
		}
		return SUCCESS;
	}
	
	// 新建文件夹
	public void makeNewFileWork() throws Exception{
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest req = ServletActionContext.getRequest();
			String fileName = req.getParameter("filePath");
			try {
				GetOssFileListUtil.addNewFile(fileName);
				this.writeJSON("ok");
			} catch (Exception e) {
				this.writeJSON("no");
			}
		} else {
			this.writeJSON("no");
		}
	}
	
	
	// 删除文件
	public void delOssFileInfo() throws Exception{
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest req = ServletActionContext.getRequest();
			String fileName = req.getParameter("filePath");
			try {
				GetOssFileListUtil.delAppointPrefix(fileName);
				this.writeJSON("ok");
			} catch (Exception e) {
				this.writeJSON("no");
			}
		} else {
			this.writeJSON("no");
		}
	}
	
	private File[] uploadImage; //得到上传的文件
    private String[] uploadImageFileName;//文件名称
	public void uploadFile() throws Exception{// 上传文件
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest req = ServletActionContext.getRequest();
			String filePath = "";
			
			if (req.getParameter("filePath")!=null) {
				filePath = req.getParameter("filePath");
			}
			
			try {
				if (uploadImage.length>0) {// 多个文件
					InputStream inputStream = null;
					for (int i = 0; i < uploadImage.length; i++) {
						File file = uploadImage[i];
						String fileName = uploadImageFileName[i];
						inputStream = new FileInputStream(file);
						GetOssFileListUtil.uploadFile(filePath+fileName, inputStream);
					}
					this.writeJSON("ok");
				} else {
					this.writeJSON("no");
				}
			} catch (Exception e) {
				this.writeJSON("no");
			}
		} else {
			this.writeJSON("no");
		}
	}
	
	
	public void downFile() throws Exception{// 下载文件
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest req = ServletActionContext.getRequest();
			String filePath = req.getParameter("filePath");// image/
			String file = req.getParameter("file");// 文件名称 image/1.png
			
			Date date = new Date();
			SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMddHHmmss");
			String dataStr2 = format2.format(date);
			HttpServletResponse response = ServletActionContext.getResponse();
			
			try {
				String fileName = "";
				if (filePath==null || filePath.length()<=0) {
					fileName = dataStr2+file;
				} else {
					fileName = dataStr2+file.replaceAll(filePath, "");
				}
				response.reset();
				response.setContentType("application/x-msdownload");
				response.setHeader("Content-Disposition","attachment;filename=" + new String(fileName.getBytes("gb2312"),"ISO8859-1"));
				// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>oss下载<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
				// 创建OSSClient实例。
				OSS ossClient = new OSSClientBuilder().build(GetOssFileListUtil.endpoint, GetOssFileListUtil.accessKeyId,GetOssFileListUtil.accessKeySecret);// 上传文件流。				key = fileUrl.toString();
//				key = key.replaceAll("\\\\", "/");
				OSSObject ossObject = ossClient.getObject(GetOssFileListUtil.bucketName, file);
				BufferedInputStream input = new BufferedInputStream(ossObject.getObjectContent());
				byte[] buffBytes = new byte[1024];
				 
				OutputStream outputStream = response.getOutputStream();
				 
				int read = 0;
				while ((read = input.read(buffBytes)) != -1) {
				     outputStream.write(buffBytes, 0, read);
				}
				 
				outputStream.flush();
				outputStream.close();
				//is.close();
				input.close();
				ossClient.shutdown();
			} catch (Exception e) {
				this.writeJSON("no");
			}
		} else {
			this.writeJSON("no");
		}
	}
	
	
	// 实体set和get方法
	public File[] getUploadImage() {
		return uploadImage;
	}
	public String[] getUploadImageFileName() {
		return uploadImageFileName;
	}
	public void setUploadImageFileName(String[] uploadImageFileName) {
		this.uploadImageFileName = uploadImageFileName;
	}

	public void setUploadImage(File[] uploadImage) {
		this.uploadImage = uploadImage;
	}

	public String getFindPrefix() {
		return findPrefix;
	}

	public void setFindPrefix(String findPrefix) {
		this.findPrefix = findPrefix;
	}

	public String getFindNextMark() {
		return findNextMark;
	}

	public void setFindNextMark(String findNextMark) {
		this.findNextMark = findNextMark;
	}

	public String getFindMobStat() {
		return findMobStat;
	}
	public void setFindMobStat(String findMobStat) {
		this.findMobStat = findMobStat;
	}
	public String getFindSdt() {
		return findSdt;
	}
	public void setFindSdt(String findSdt) {
		this.findSdt = findSdt;
	}
	public UserInfoDto getUserInfo() {
		return userInfo;
	}
	public void setUserInfo(UserInfoDto userInfo) {
		this.userInfo = userInfo;
	}
	public UserInfoService getUserInfoService() {
		return userInfoService;
	}
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	public String getFindMobile() {
		return findMobile;
	}
	public void setFindMobile(String findMobile) {
		this.findMobile = findMobile;
	}
	public String getFindName() {
		return findName;
	}
	public void setFindName(String findName) {
		this.findName = findName;
	}
	public String getFindId() {
		return findId;
	}
	public void setFindId(String findId) {
		this.findId = findId;
	}
	public Map<String, Object> getMapResult() {
		return mapResult;
	}
	public void setMapResult(Map<String, Object> mapResult) {
		this.mapResult = mapResult;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public List<UserInfoDto> getListUser() {
		return listUser;
	}
	public void setListUser(List<UserInfoDto> listUser) {
		this.listUser = listUser;
	}
	public String getFindType() {
		return findType;
	}
	public void setFindType(String findType) {
		this.findType = findType;
	}

	public String getFindDateStart() {
		return findDateStart;
	}

	public void setFindDateStart(String findDateStart) {
		this.findDateStart = findDateStart;
	}

	public String getFindDateEnd() {
		return findDateEnd;
	}
	public void setFindDateEnd(String findDateEnd) {
		this.findDateEnd = findDateEnd;
	}
	public String getFindIsPay() {
		return findIsPay;
	}
	public void setFindIsPay(String findIsPay) {
		this.findIsPay = findIsPay;
	}
	
}
